package models;
import java.text.ParseException;
import java.util.Date;

/**
 * A Workshop object contains data about a workshop.
 */
public class Workshop extends DatabaseModel implements Searchable {

    /**
     * Een verzameling van vaste dagdelen en een hele dag.
     */
    public enum DagDeel {
        VOORMIDDAG("Voormiddag"),
        HELEDAG("Hele dag"),
        NAMIDDAG("Namiddag");
        
        final public String beautifulString;
        DagDeel(String beatifulString) {
        	this.beautifulString = beatifulString;
        }
        
        /**
         * Parse a String. It will be recognized if it is equal to the Enum name or 
         * the beautiful string representation (case in-sensitive).
         * @param dagDeel String to parse
         * @return DagDeel found
         * @throws ParseException if not parsed successfully
         */
        public static DagDeel parse(String dagDeel) throws ParseException {
			if(dagDeel.equalsIgnoreCase("VOORMIDDAG") || dagDeel.equalsIgnoreCase(VOORMIDDAG.beautifulString)) {
				return VOORMIDDAG;
			}
			else if(dagDeel.equalsIgnoreCase("HELEDAG") || dagDeel.equalsIgnoreCase(HELEDAG.beautifulString)) {
		        return HELEDAG;
			}
			else if(dagDeel.equalsIgnoreCase("NAMIDDAG") || dagDeel.equalsIgnoreCase(NAMIDDAG.beautifulString)) {
		        return NAMIDDAG;
			}
			else {
				throw new ParseException(dagDeel, 0);
			}
        }
    }
    
    private String title;
    private Date date;
    private DagDeel dagDeel;
    private double price;
    private String explaination;
    private int participantsMax;
    private String docent;

    /**
     * Create a Workshop with default values.
     */
    public Workshop(){
    	this("", getTodaysDate(), DagDeel.HELEDAG, 0.0, "", 10, "");
    }
    
    /**
     * Create a workshop filled with data.
     * 
     * @param title of the workshop
     * @param date of the workshop 
     * @param dagDeel of the workshop 
     * @param price of the workshop 
     * @param explaination of the workshop 
     * @param participantsMax Maximum amount of participants
     * @param docent of the workshop
     */
    public Workshop(String title, Date date, DagDeel dagDeel, double price, String explaination, int participantsMax, String docent) {
        this.title = title;
        this.date = date;
        this.dagDeel = dagDeel;
        this.price = price;
        this.explaination = explaination;
        this.participantsMax = participantsMax;
        this.docent = docent;
    }
        
    /**
     * Same as {@link #Workshop(String title, Date date, DagDeel dagDeel, double price, String explaination, int participantsMax, String docent)}
     * but with the id added (generated by the database).
     */
    public Workshop(int id, String title, Date date, DagDeel dagDeel, double price, String explaination, int participantsMax, String docent) {
        this(title, date, dagDeel, price, explaination, participantsMax, docent);
        setId(id);
    }
    
    /**
     * Create a copy of this workshop (data only, not the ID).
     * 
     * @return new Workshop with the same data.
     */
    public Workshop copy() {
        Workshop copyOfWorkshop = new Workshop(title, date, dagDeel, price, explaination, participantsMax, docent);
        return copyOfWorkshop;
    }
	
    /**
     * {@inheritDoc}
     */
	@Override
	public boolean isSearchMatch(String searchString) {
		return (
			title.toLowerCase().contains(searchString.toLowerCase()) ||
			getDateString().toLowerCase().contains(searchString.toLowerCase()) ||
			("" + price).contains(searchString.toLowerCase()) ||
			explaination.toLowerCase().contains(searchString.toLowerCase()) ||
			docent.toLowerCase().contains(searchString.toLowerCase())
		);
	}
	
    /**
     * {@inheritDoc}
     */
	@Override
	public String[] getDataHeaders() {
		String[] headers = { "title", "date", "dagdeel", "price", "explaination", "participantsMax", "docent" };
		return headers;
	}
	
    /**
     * {@inheritDoc}
     */
	@Override
	public String[] getData() {
		String[] data = { this.title, getDateString(), ""+this.dagDeel, ""+this.price, this.explaination, ""+this.participantsMax, this.docent};
		return data;
	}	

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
	
	public String getDateString(){
	    return dbDateFormat.format(date);
	}
	
	public void setDate(Date date) {
		this.date = date;
	}
	
	public Date getDate(){
		return date;
	}
	
	public DagDeel getDagDeel() {
		return dagDeel;
	}

	public void setDagDeel(DagDeel dagDeel) {
		this.dagDeel = dagDeel;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public String getExplaination() {
		return explaination;
	}

	public void setExplaination(String explanation) {
		this.explaination = explanation;
	}

	public int getParticipantsMax() {
		return participantsMax;
	}

	public void setParticipantsMax(int participantsMax) {
		this.participantsMax = participantsMax;
	}

	public String getDocent() {
		return docent;
	}

	public void setDocent(String docent) {
		this.docent = docent;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((dagDeel == null) ? 0 : dagDeel.hashCode());
		result = prime * result + ((date == null) ? 0 : date.hashCode());
		result = prime * result + ((docent == null) ? 0 : docent.hashCode());
		result = prime * result
				+ ((explaination == null) ? 0 : explaination.hashCode());
		result = prime * result + participantsMax;
		long temp;
		temp = Double.doubleToLongBits(price);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		result = prime * result + ((title == null) ? 0 : title.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		Workshop other = (Workshop) obj;
		if (dagDeel != other.dagDeel)
			return false;
		if (date == null) {
			if (other.date != null)
				return false;
		} else if (!date.equals(other.date))
			return false;
		if (docent == null) {
			if (other.docent != null)
				return false;
		} else if (!docent.equals(other.docent))
			return false;
		if (explaination == null) {
			if (other.explaination != null)
				return false;
		} else if (!explaination.equals(other.explaination))
			return false;
		if (participantsMax != other.participantsMax)
			return false;
		if (Double.doubleToLongBits(price) != Double
				.doubleToLongBits(other.price))
			return false;
		if (title == null) {
			if (other.title != null)
				return false;
		} else if (!title.equals(other.title))
			return false;
		return true;
	}
	
}